#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > cc_rsp_exci.info <<'%EOF%'
   cc_rsp_exci
   -----------
   Molecule:         HeH+ in C2
   Wave Function:    CCSD / 6-31G**
   Test Purpose:     Excited states linear response function
%EOF%

#######################################################################
#  INTEGRAL INPUT
#######################################################################
cat > cc_rsp_exci.mol <<'%EOF%'
BASIS
6-31G** 
HeH+

    2    1  X         1.00D-15
        2.    1    
He    0.0000000000000000  0.0000000000000000  1.4632000000000000       *
        1.    1    
H     0.0000000000000000  0.0000000000000000  0.00000000000000         *
%EOF%



#######################################################################
#  DALTON INPUT
#######################################################################
cat > cc_rsp_exci.dal <<'%EOF%'
**DALTON INPUT
.RUN WAVE FUNCTIONS
**INTEGRAL
.DIPLEN
.THETA
.EFGCAR
.ANGMOM
**WAVE FUNCTIONS
.CC
*OPTIMI
.DETERM
.THRESHOLD
 1.0d-10
*SCF INPUT      
.DOUBLY OCCUPIED
 1 0 0 0
.THRESH
  1.0d-10
*CC INPUT
.CCSD
.NSIMLE
  1
.NSYM
  2
.THRLEQ
 1.0d-10
.THRENR
 1.0d-10
.PRINT
  5
*CCEXCI
.NCCEXCI
 1 2 
.THREXC
 1.0d-12
*CCFOP
.DIPMOM
*CCLR
.OLD_LR
.DIPOLE
*CCEXLR
.USELEFT
.OPERATOR
ZDIPLEN ZDIPLEN
XDIPLEN XDIPLEN
YDIPLEN YDIPLEN
.SELSTA
 1 1 1 1
.FREQUE
   2
 0.000 0.0235
**END OF DALTON INPUT
%EOF%

#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >cc_rsp_exci.check
cat >>cc_rsp_exci.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# SCF, MP2 and CCSD energies:
CRIT1=`$GREP "Final * HF energy:" $log | $GREP "\-2.9247056999" | wc -l`
CRIT2=`$GREP "Total MP2   energy:                    \-2.95349954" $log | wc -l`
CRIT3=`$GREP "Total CCSD  energy:                    \-2.96120495" $log | wc -l`
TEST[1]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3` 
CTRL[1]=5
ERROR[1]="ENERGIES NOT CORRECT"

# CCSD excitation energies
CRIT1=`$GREP "\| \^1A.  \|    1   \|     ( |0)\.976548.  \|      26\.5732.  \|    214327\.6..  \|  96\.51" $log | wc -l`
CRIT2=`$GREP  "\| \^1A.  \|    1   \|     2\.360274.  \|      64\.2263.  \|    518020\.4..  \|  97\.41" $log | wc -l`
CRIT3=`$GREP  "\| \^1A.  \|    2   \|     3\.133336.  \|      85\.2624.  \|    687687\.9..  \|  66\.99" $log | wc -l`
TEST[2]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3` 
CTRL[2]=3
ERROR[2]="CCSD EXCITATION ENERGIES NOT CORRECT"

# CCSD excited state polarizabilities
# ZDIPLEN   .0000   ZDIPLEN   .0000     -.41777707
#          -.0235             .0235     -.41688348
# XDIPLEN   .0000   XDIPLEN   .0000     -.46661696E-01
#          -.0235             .0235     -.46664081E-01
# YDIPLEN   .0000   YDIPLEN   .0000     -.46661696E-01
#          -.0235             .0235     -.46664081E-01               
CRIT1=`$GREP "ZDIPLEN *\-*0*\.0000 * ZDIPLEN *\-*0*\.0000 * \-0*\.41777..." $log | wc -l`
CRIT2=`$GREP  "[ ]+( \-|\-0)\.0235            ( |0)\.0235    ( \-|\-0)\.41688..." $log | wc -l`
CRIT3=`$GREP "XDIPLEN (\-0| \-|  | 0)\.0000   XDIPLEN (\-0| \-|  | 0)\.0000    ( \-|\-0)\.46661...(E|D)\-01" $log | wc -l`
CRIT4=`$GREP  "[ ]+( \-|\-0)\.0235            ( |0)\.0235    ( \-|\-0)\.46664...(E|D)\-01" $log | wc -l`
CRIT5=`$GREP "YDIPLEN (\-0| \-|  | 0)\.0000   YDIPLEN (\-0| \-|  | 0)\.0000    ( \-|\-0)\.46661...(E|D)\-01" $log | wc -l`
TEST[3]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3  \+ $CRIT4 \+ $CRIT5` 
CTRL[3]=6
ERROR[3]="CCSD EXCITED STATE POLARIZABILITIES NOT CORRECT"

# Norm of EL1 vectors (tests the "projection")
#    4     ZDIPLEN      1       1         0.9765486861  -0.0235000000   1.2255167405   0.4959190808
#    5     ZDIPLEN      1       1         0.9765486861   0.0000000000   1.1359434151   0.4761680199
#    6     ZDIPLEN      1       1         0.9765486861   0.0235000000   1.0563509932   0.4579313280             
CRIT1=`$GREP  "Testing EL1 list with  9 elements on it" $log | wc -l`
CRIT2=`$GREP "4[ ]+ZDIPLEN[ ]+1[ ]+1[ ]* ( |0)\.97654868..  ( \-|\-0)\.0235[0]*   1\.225516....   ( |0)\.4959190" $log | wc -l`
CRIT3=`$GREP "5[ ]+ZDIPLEN[ ]+1[ ]+1[ ]* ( |0)\.97654868..  (\-0| \-|  | 0)\.0000[0]*   1\.135943....   ( |0)\.4761680" $log | wc -l`
CRIT4=`$GREP "6[ ]+ZDIPLEN[ ]+1[ ]+1[ ]* ( |0)\.97654868..   ( |0)\.0235[0]*   1\.05635.*   ( |0)\.4579313" $log | wc -l`
TEST[4]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3  \+ $CRIT4`
CTRL[4]=4
ERROR[4]="PROJECTION OF EQUATIONS NOT CORRECT"

# CCSD ground state polarizabilities:
#<<XDIPLEN ,XDIPLEN >>(  .000000) =     -.40286062
#<<YDIPLEN ,YDIPLEN >>(  .000000) =     -.40286062
#<<ZDIPLEN ,ZDIPLEN >>(  .000000) =    -1.47136020                 
CRIT1=`$GREP "<<XDIPLEN ,XDIPLEN >>\( ( |0)\.000000\) =    ( \-|\-0)\.40286062" $log | wc -l`
CRIT2=`$GREP "<<YDIPLEN ,YDIPLEN >>\( ( |0)\.000000\) =    ( \-|\-0)\.40286062" $log | wc -l`
CRIT3=`$GREP "<<ZDIPLEN ,ZDIPLEN >>\( ( |0)\.000000\) =    \-1\.47136020" $log | wc -l`
TEST[5]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[5]=3
ERROR[5]="CCSD GROUND STATE POLARIZABILITIES NOT CORRECT"


PASSED=1
for i in 1 2 3 4 5
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi                
%EOF%
#######################################################################
